#!/bin/csh -f
#
#         C-shell script : CRZ2RNX
#             (frontend of CRX2RNX)
#                 1996.12.19  created by Y. Hatanaka.
#                 2007.06.10  updated by Y. Hatanaka.
#                      - replace "compress" with "uncompress"
#                      - file name extension may be lower and upper case
#                 2009.07.07  modified extensively by Y. Hatanaka.
#                      - Ambiguities/bugs in following items are resolved:
#                          * setting of output directory
#                          * case of deletion of input files
#                          * case of overwriting an output file
#                        Options to control them are added.
#                      - Handling of more RINEX file types are added.
#                      - 'gzipped' files can be processed.
#                 2014.03.24  modified by Y. Hatanaka.
#                      - Manipulation of file names in the new file naming
#                        convention (*.rnx/crx) is added.
#
#--------------------------------------------------------------------
set help = 0
foreach var ($argv[*])
   if ( "$var" == '-h' ) set help = 1
end

if($#argv < 1 || $help) then
more << EOF

CRZ2RNX : C-shell script to decompress multiple RINEX files.

   Usage : CRZ2RNX [-c] [-d] [-f] [-v] file ...

       -c : output to the current directory
       -d : delete input files if decompressed successfully
       -f : force overwriting output files without inquiring
       -v : verbose mode
       -h : show this message and stop
       file ... : input compressed RINEX (or CRINEX) files. 
                  Wildcards can be used.

  compressed RINEX/CRINEX -->     CRINEX     -->      RINEX
                               ????????.??d  -->   ????????.??o
       ????????.??d.Z(gz) --> (????????.??d) -->   ????????.??o
       ????????.??o.Z(gz)                    -->   ????????.??o
       ????????.??n.Z(gz)                    -->   ????????.??n
       ????????.??g.Z(gz)                    -->   ????????.??g
       ????????.??l.Z(gz)                    -->   ????????.??l
       ????????.??p.Z(gz)                    -->   ????????.??p
       ????????.??h.Z(gz)                    -->   ????????.??h
       ????????.??b.Z(gz)                    -->   ????????.??b
       ????????.??m.Z(gz)                    -->   ????????.??m
       ????????.??c.Z(gz)                    -->   ????????.??c
       *.?O.crx.Z(gz)                        -->    *.?O.rnx
       *.rnx.Z(gz)                           -->    *.rnx

Remarks:
  - Installation of CRX2RNX is necessary to use this tool.
  - The extensions of the input files must conform to the RINEX convention.
  - An decompressed file is saved in the same directory as the input file
    unless the option '-c' is specified.
  - An input file is deleted only when the option "-d" is specified and
    the compression is successful.

   [20090707]

EOF

exit
endif
#--------------------------------------------------------------------

# set default mode
set out_to_current_dir = 0
set delete_input_files = 0
set force_overwrite = 0
unset verbose

set PROGRAM = CRX2RNX

unset noclobber

# check options
foreach var ($argv[*])
   switch ($var)
     case '-c':
       set out_to_current_dir = 1
       shift; breaksw
     case '-d':
       set delete_input_files = 1
       shift; breaksw
     case '-f':
       set force_overwrite = 1
       shift; breaksw
     case '-v':
       set verbose = 1
       shift; breaksw
     default:
       break
   endsw
end


# process files
foreach file_in ($argv[*])

    # compose name of output file (excluding ".Z" or ".gz")
    set ext  = $file_in:e
    if ( $file_in =~ *.Z || $file_in =~ *.gz ) then
        set file = $file_in:r  # remove ".Z" or ".gz"
        set CAT = zcat
    else
        set file = $file_in
        set CAT = cat;
    endif
    if ( $out_to_current_dir ) set file = $file:t
echo $file
    if ( $file =~ *.??[dD] ) then
        set file_out = `echo $file | sed -e 's/d$/o/' -e 's/D$/O/' `
    else if ( $file =~ *[oO].crx || $file =~ *[oO].CRX ) then
        set file_out = `echo $file | sed -e 's/crx$/rnx/' -e 's/CRX$/RNX/' `
    else if ( ($file =~ *.??[dDnNgGlLpPhHbBmMcC] ||     \
               $file =~ *.rnx || $file =~ *.RNX ) && \
                    ($ext == Z || $ext == gz) ) then
        set file_out = $file
    else
        echo "Skip $file_in. It's already uncompressed or the file name doesn't fit to the naming convention."
        continue
    endif

    # check if the output file is preexisting
    if ( -e "$file_out" && ! $force_overwrite ) then
        echo "The file $file_out already exists. Overwrite?(y/n,default:n)"
        if ( $< !~ [yY] ) continue
    endif

    # issue the command
    if      ( $file =~ *.??[dD] || $file =~ *[oO].crx || $file =~ *[oO].CRX ) then
        $CAT $file_in  | CRX2RNX - > $file_out
    else
        $CAT $file_in > $file_out
    endif

    # remove the input file
    if ( $status == 0 && $delete_input_files ) rm $file_in

end
